﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ChoViet.Lib.Common;
using System.Linq.Dynamic;
using ChoViet.Lib.Helpers;
using ChoViet.Model;
using ChoViet.CMS.ViewModels;
using ChoViet.Lib.Paging;

namespace ChoViet.CMS.Controllers
{
    public class AreaController : Controller
    {
        /// <summary>
        /// Created by: VietLV
        /// Created date: 01/05/2012
        /// Description: Manage Area controller
        /// </summary>
        /// <returns></returns>
        ChoVietDataContext eDataContext = new ChoVietDataContext();

        #region GET: /Area/IndexCms
        public ActionResult IndexCms(int? page)
        {
            try
            {
                if(page == null) page=1;
                if (UserManager.CheckRole(RoleSystem.Dictionary))
                {
                    string order = UserState.getUserStateFromRequest("area.order", "order", "ID").ToString();
                    string orderDir = UserState.getUserStateFromRequest("area.orderDir", "orderDir", "desc").ToString();
                    var param = eDataContext.Areas
                                        .OrderBy(string.Format("{0} {1}", order, orderDir))
                                        .ToPagedList(page.Value - 1, 20);

                    ViewData["orderDir"] = orderDir;
                    ViewData["order"] = order;

                    return View(new AreaIndexViewModel
                    {
                       Areas = param
                    });
                }
                else
                {
                    ViewData[Messages.MessageInfo] = Messages.NOT_PERMIT;
                    return View();
                }
            }
            catch (Exception exp)
            {
                eDataContext.Dispose();
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }
        #endregion

        #region GET: /Area/CreateCms
        public ActionResult CreateCms()
        {
            var param = new Area();
            return View();
        }
        #endregion

        #region POST: /Area/CreateCms
        [HttpPost]
        public ActionResult CreateCms(Area fromdata)
        {
            try
            {
                UpdateModel(fromdata);
                eDataContext.Areas.InsertOnSubmit(fromdata);
                eDataContext.SubmitChanges();
                return RedirectToAction("IndexCms");
            }
            catch
            {
                eDataContext.Dispose();
                return View();
            }
        }
        #endregion

        #region GET: /Area/EditCms/5
        public ActionResult EditCms(int id)
        {
            try
            {
                var param = eDataContext.Areas.Where(e => e.ID == id).FirstOrDefault();   
                return View(param);
            }
            catch (Exception exp)
            {
                eDataContext.Dispose();
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }
        #endregion

        #region POST: /Area/EditCms/5
        [HttpPost]
        public ActionResult EditCms(int id, Area c)
        {
            try
            {
                c = eDataContext.Areas.Where(e => e.ID == id).SingleOrDefault();
                UpdateModel(c);
                eDataContext.SubmitChanges();
                return RedirectToAction("IndexCms");
            }
            catch (Exception exp)
            {
                eDataContext.Dispose();
                ViewData[Messages.MessageInfo] = exp.Message;
                return View();
            }
        }
        #endregion

        #region Site: GetAreaFooter
        [ChildActionOnly]
        public ActionResult GetAreaFooter()
        {
            var param = eDataContext.Areas.ToList();
            return View(param);
        }
        #endregion
    }
}
